#include <time.h>

#include "Common.h"
#include "LinkedList.h"
#include "DetectFinger.h"
#include "ImgProc.h"
#include "LookupTable.h"

#define MAIN_FUNCTION 3

#define WINDOW1 "handpie window 1"
#define WINDOW2 "handpie window 2"
#define WINDOW3 "handpie window 3"

void remoteControl(int numberOfFingers, coordinate_t nextPoint);
int groupingOfFingers(IplImage* image, queue_t* unitQueue[], int numberOfQueue);
void numberOfFingers(queue_t* unitQueue[], int numberOfThread);
void horizontalFlipFlop(IplImage* image, int width, int height);
coordinate_t checkMoving(IplImage* image, queue_t* movingQueue, coordinate_t point);
void deleteArm(	IplImage* image, queue_t* unitQueue[] ,int numberOfQueue,	coordinate_t point);
void mainSangin(LPBYTE lpInput, LPWORD lpHue, LPBYTE lpSat, LPBYTE lpInt, int width, int height);
void mainGJKang(LPBYTE lpInput, LPWORD lpHue, LPBYTE lpSat, LPBYTE lpInt, int width, int height);
void mainKyusik();

#define NUM_FINGER 20

int countFingers[NUM_FINGER] = {0, };

int main(int argc, char** argv) {
#if   MAIN_FUNCTION==4
	// OpenCV variables
	CvCapture* capture;
	IplImage* image;  // input frame
	IplImage* output1;  // output frame
	IplImage* output2;  // output frame
	IplImage* output3;  // output frame

	// Image buffers
	LPWORD imageRgb565;
	LPWORD imageHue;
	LPBYTE imageSaturation;
	LPBYTE imageIntensity;

	/*
	 * temporary variables
	 */
	int resolution;
	int i, j, index;
	WORD currentHue;
	BYTE currentSat;
	BYTE currentInt;

	/*
	 * running time measuring variables
	 */
	clock_t startTime = 0L;
	double elapsedTime = 0;
	char frames = 0;
	int timing = 0;

	/***************
	 * main start
	 ***************/
	capture = cvCaptureFromCAM(0);

	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, ORG_WIDTH);
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, ORG_HEIGHT);
	cvNamedWindow(WINDOW1, CV_WINDOW_AUTOSIZE);
	cvNamedWindow(WINDOW2, CV_WINDOW_AUTOSIZE);
	cvNamedWindow(WINDOW3, CV_WINDOW_AUTOSIZE);

	if (!cvGrabFrame(capture)) {
		printf("camera is NUll");
	}
	image = cvRetrieveFrame(capture);
	output1 = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
	output2 = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
	output3 = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);

	/*
	 * memory allocation
	 */
	resolution = image->width * image->height;
	imageRgb565 = (LPWORD) malloc(sizeof(WORD) * resolution);
	imageHue = (LPWORD) malloc(sizeof(WORD) * resolution);
	imageSaturation = (LPBYTE) malloc(sizeof(BYTE) * resolution);
	imageIntensity = (LPBYTE) malloc(sizeof(BYTE) * resolution);

	/*
	 * processing each frames
	 */
	while (cvWaitKey(10) < 0) {
		startTime = clock();

		cvGrabFrame(capture);
		image = cvRetrieveFrame(capture);

		// convert rgb to hsi
		//rgb888to565((LPBYTE) image->imageData, imageRgb565, image->width, image->height);
		//rgb2hsi(imageRgb565, imageHue, imageSaturation, imageIntensity, image->width, image->height);
		rgb888toHsi((LPBYTE) image->imageData, imageHue, imageSaturation, imageIntensity, image->width,
				image->height);
#endif 

		// 여기에 자신의 mainName() 함수를 사용하면 됨
#if MAIN_FUNCTION==0
		mainGJKang((LPBYTE)image->imageData, imageHue, imageSaturation, imageIntensity, image->width, image->height);
#elif MAIN_FUNCTION==1
#elif MAIN_FUNCTION==2
#elif MAIN_FUNCTION==3
		mainKyusik();
#elif MAIN_FUNCTION==4
		mainSangin((LPBYTE) image->imageData, imageHue, imageSaturation, imageIntensity, image->width,
				image->height);
#endif	

#if   MAIN_FUNCTION==4
		// 여기에 자신의 mainName() 함수를 사용하면 됨
		output2->imageData = (char*) imageHue;
		output3->imageData = (char*) imageSaturation;

		cvShowImage(WINDOW1, image);
		cvShowImage(WINDOW2, output2);
		cvShowImage(WINDOW3, output3);

		// fps 계산
		frames++;
		elapsedTime = (elapsedTime + ((double) (clock() - startTime) / CLOCKS_PER_SEC));
		if (elapsedTime >= 1.0) {
			printf("fps: %d\n", frames);
			elapsedTime -= 1.0;
			frames = 0;
		}
	}

	/*
	 * release allocation & resources
	 */
	free(imageRgb565);
	free(imageHue);
	free(imageSaturation);
	free(imageIntensity);
	cvReleaseImage(&output1);
	cvReleaseImage(&output2);
	cvReleaseImage(&output3);
	cvReleaseCapture(&capture);
	cvDestroyWindow(WINDOW1);
	cvDestroyWindow(WINDOW2);
	cvDestroyWindow(WINDOW3);
#endif
	return 0;

}

void mainSangin(LPBYTE lpInput, LPWORD lpHue, LPBYTE lpSat, LPBYTE lpInt, int width, int height) {
}

void mainGJKang(LPBYTE lpInput, LPWORD lpHue, LPBYTE lpSat, LPBYTE lpInt, int width, int height) {
}



void mainKyusik() {
	int valueR, valueG, valueB, i, j;
	int x1, x2, y1, y2;
	int x, y;
	coordinate_t point;
	areaCoordinate_t interestedArea;
	int range, r;
	int startAndEnd = 0;
	int numberOfThread = 15;
	pthread_t tid[50];
	argumentDataSet_t* argumentsData[50];
	queue_t* unitQueue[50];
	queue_t* movingQueue = NULL;
	initQueue(movingQueue);


	//memory allocation for thread arguments
	for (int i = 0; i < numberOfThread; i++)
	{   
		argumentsData[i] = (argumentDataSet_t*) malloc(sizeof(argumentDataSet_t));
		//unitQueue[i] = (queue_t*) malloc ( sizeof(queue_t));
	}   
	/*
	   for ( int i = 0; i < numberOfThread; i++ )
	   {
	   unitQueue[i] = (queue_t*) malloc ( sizeof(queue_t));
	   }
	 */

	IplImage *image;
	CvCapture *capture;

	IplImage *image_binary;
	capture = cvCaptureFromCAM(0);
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, IMG_WIDTH);
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, IMG_HEIGHT);

	if (!cvGrabFrame(capture)) {
		printf("camera is NUll");
	}
	cvNamedWindow("camera1", 0);
	cvNamedWindow("camera2", 0);
	cvResizeWindow("camara1", 640, 480);
	cvResizeWindow("camera2", 640, 480);

	while (cvWaitKey(10) < 0) {
		cvGrabFrame(capture);
		image = cvRetrieveFrame(capture);
		horizontalFlipFlop(image, IMG_WIDTH, IMG_HEIGHT);
		image_binary = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 3);
		//printf("getsizs : %d, ipl: %d\n", cvGetSize(image), IPL_DEPTH_8U);


		for ( int l = 0; l < numberOfThread; l++ )
		{
			initQueue(unitQueue[l]);
		}

		x1 = 9999;
		x2 = 0;
		y1 = 9999;
		y2 = 0;
		for (i = 0; i < IMG_HEIGHT; i++) {
			for (j = 0; j < IMG_WIDTH; j++) {
				int temp = (i * IMG_WIDTH + j) * 3;
				valueR = image->imageData[temp + 2];
				valueG = image->imageData[temp + 1];
				valueB = image->imageData[temp + 0];

				if (valueR > 95 && valueG > 40 && valueB > 20 &&
						MAX(valueR, MAX(valueG, valueB)) - MIN(valueR, MIN(valueG, valueB)) > 15 &&
						ABS(valueR, valueG) > 15 && valueR > valueG && valueR > valueB) {
					image_binary->imageData[temp + 2] = 255;
					image_binary->imageData[temp + 1] = 255;
					image_binary->imageData[temp] = 255;
				} else {
					image_binary->imageData[temp + 2] = 0;
					image_binary->imageData[temp + 1] = 0;
					image_binary->imageData[temp] = 0;
				}

			}

		}

		erodeImage(image_binary);

		for (i = 0; i < IMG_HEIGHT; i++) {
			for (j = 0; j < IMG_WIDTH; j++) {
				int temp = (i * IMG_WIDTH + j) * 3;
				if (image_binary->imageData[temp] == 255) {
					x1 = MIN(j, x1);
					x2 = MAX(j, x2);
					y1 = MIN(i, y1);
					y2 = MAX(i, y2);
				}
			}
		}

		cvRectangle(image_binary, cvPoint(x1, y1), cvPoint(x2, y2), CV_RGB(255, 0, 0), 2, 2, 0);
		//printf("%d %d %d %d", x1, y1, x2-x1, y2-y1);

		interestedArea.point1.x = x1;
		interestedArea.point1.y = y1;
		interestedArea.point2.x = x2;
		interestedArea.point2.y = y2;

		findCentre(image_binary, point, interestedArea);

		//printf("%d %d\n", point.x, point.y);

		//center
		cvCircle(image_binary, cvPoint(point.x, point.y), 10, CV_RGB(0, 255, 0), -1);

		//start threads
		for (int i = 0; i < numberOfThread; i++)
		{
			setArguments(argumentsData[i], image_binary, unitQueue[i], interestedArea, point, 0.1 + i* 0.05);
			pthread_create(&tid[i], NULL, detectFinger, (void*) argumentsData[i]);
		}


		for (int i = 0; i < numberOfThread; i++)
		{
			pthread_join(tid[i], NULL);	
		}


		//deleteArm(image_binary, unitQueue, numberOfThread, point);

		//check number of fingers
		//numberOfFingers(unitQueue, numberOfThread);

		
		//checkMoving
		coordinate_t nextPoint = checkMoving(image_binary, movingQueue, point);

		int counting =  groupingOfFingers(image_binary, unitQueue, numberOfThread);		
		remoteControl(counting, nextPoint);



		//printf("%d\n", numberOfFingers);

		for( int i = 0; i < numberOfThread; i++)
		{
			freeQueue(unitQueue[i]);
		}



		//printf("------------\n");
		//getchar();
		cvShowImage("camera1", image_binary);
		cvShowImage("camera2", image);
		cvReleaseImage(&image_binary);
	}

	freeQueue(movingQueue);
	cvReleaseCapture(&capture);
	cvDestroyWindow("camera1");
	cvDestroyWindow("camera2");
	return;
}

void remoteControl(int numberOfFingers, coordinate_t nextPoint)
{

	for ( int i = 0; i < NUM_FINGER; i++)
	{			
		if ( numberOfFingers == i ) 
		{
			countFingers[i]++;				
			if (countFingers[i] > 5)
			{
				//printf("%d\n", i);
				if (countFingers[i] > 10)
				{										
					//printf("nextPoint : %d, %d\n", nextPoint.x, nextPoint.y);
					int x = nextPoint.x;
					int y = nextPoint.y;
					int absX = abs(x);
					int absY = abs(y);
					

					if ( absX > absY ) 
					{
						if ( x < -10 )
						{
							printf("%d left\n", i);
							countFingers[i] = 5;
						}
						else if ( x > 10 )
						{
							printf("%d right\n", i);
							countFingers[i] = 5;							
						}
					}
					else
					{
						if ( y < -10 )
						{
							printf("%d up\n", i);
							countFingers[i] = 5;
						}
						else if ( y > 10 )
						{
							printf("%d down\n", i);
							countFingers[i] = 5;							
						}
					}
				}
				
			}
		}
		else
		{
			countFingers[i] = 0;
		}
	}
}


int groupingOfFingers(IplImage* image, queue_t* unitQueue[], int numberOfQueue)
{
	int group = 1;
	float groupDistance[1000] = {0, };
	queue_t* tempQueue[50];
   	
	//copy array of queue
	for ( int i = 0; i < numberOfQueue; i++)
	{
		initQueue(tempQueue[i]);
		for ( int j = 0; j < unitQueue[i]->length; j++)
		{
			node_t* tempNode = nodeAtQueue(unitQueue[i], j);
			if ( tempNode->data < 50 ) 
				enqueue(tempQueue[i], 0, tempNode->point);
		}
	}	

	//grouping from outter line to inner line
	for ( int i = numberOfQueue -1; i > 0; i--)
	{
		//if the line across fingers, start grouping
		if (tempQueue[i]->length > 0)
		{
			//the point which are acrossed by the line
			for( int k = 0; k < tempQueue[i]->length; k++)
			{

				node_t* baseTempNode = nodeAtQueue(tempQueue[i], k);

				//group start by 1 and data of node is used to group in this function.
				//if the data is 0, the node is not grouped, then  process below code.
				if (baseTempNode->data == 0)
				{
					baseTempNode->data = group;
					group++;
				}

				float x, y;
				float minUnitVector = 9999;
				float tempUnitVector = 0;
				int minNodeNumber = -1;						

				//find the nearest point which is not grouped.
				for ( int j = 0; j < tempQueue[i-1]->length; j++)
				{
					node_t* tempNode = nodeAtQueue(tempQueue[i-1], j);
					x = tempNode->point.x - baseTempNode->point.x;
					y = tempNode->point.y - baseTempNode->point.y;


					tempUnitVector = sqrt(x*x + y*y);
					
					if( tempUnitVector < minUnitVector )
					{
						minUnitVector = tempUnitVector;
						minNodeNumber = j;
					}
				}

				if( minNodeNumber != -1 && minUnitVector < 25)
				{
					node_t* tempNode = nodeAtQueue(tempQueue[i-1], minNodeNumber);

					if ( tempNode->data == 0)
					{
					
						tempNode->data = baseTempNode->data;

						groupDistance[baseTempNode->data] += minUnitVector;

						cvLine(image,
								cvPoint(baseTempNode->point.x, baseTempNode->point.y),
								cvPoint(tempNode->point.x, tempNode->point.y), 
								CV_RGB(70,70, 30), 2);
					}
				}
				
			}
			
		}
	}

	group--;	
	
	queue_t* groupQueue = NULL;
	initQueue(groupQueue);

	coordinate_t point;
	point.x = 0;
	point.y = 0;

	for( int i = 0; i < group; i++)
	{
		enqueue(groupQueue, 0, point);
	}

	//display
	for ( int i = 0; i < numberOfQueue; i++)
	{
		for ( int j = 0; j < tempQueue[i]->length;j++)
		{
			node_t* tempNode = nodeAtQueue(tempQueue[i], j);

			if (tempNode->data % 3 == 0)
				cvCircle(image, cvPoint(tempNode->point.x, tempNode->point.y), 3, CV_RGB(255, 0, 0), -1);
			else if (tempNode->data % 3 == 1)
				cvCircle(image, cvPoint(tempNode->point.x, tempNode->point.y), 3, CV_RGB(0, 255, 0), -1);
			else
				cvCircle(image, cvPoint(tempNode->point.x, tempNode->point.y), 3, CV_RGB(0, 0, 255), -1);

			nodeAtQueue(groupQueue, tempNode->data-1)->data++;
		}
	}


	int count = 0;
	for ( int i = 1; i < group; i++)
	{
		if ( 30  < groupDistance[i] )
		{
			count++;
		}
		//printf("group %d : %f\n", i, groupDistance[i]);
	}

	//printf(" %d of %d\n", count, group);
	//printf("--------------------\n");

	

	/*
	int sum = 0;
	int avg = 0;
	int* arrayOfGroup = (int*) malloc ( sizeof(int) * group );
	int count = 0;
	
	for( int i = 0; i < group; i++)
	{
		node_t* tempNode = nodeAtQueue(groupQueue, i);
		*(arrayOfGroup + i) = tempNode->data;
		sum += tempNode->data;
		//printf("group %d : %d\n", i+1, nodeAtQueue(groupQueue, i)->data);
	}
	if( group != 0 ) 
		avg = group / 2;

	for ( int i = 0; i < group; i++)
	{
		if( *(arrayOfGroup + i) > avg)
			count++;
	}


	printf("%d\n", count);
	printf("-------------------\n");
	*/



	for ( int i = 0; i < numberOfQueue; i++)
	{
		freeQueue(tempQueue[i]);
	}
	freeQueue(groupQueue);

	return count;
		
}

void numberOfFingers(queue_t* unitQueue[], int numberOfThread)
{
	int first	= 0;
	int	second	= 0;
	int third	= 0;

	//check each length of three queue. max length among lengths maybe number of fingers.
	for( int i = numberOfThread - 1 ; i >= 0  ; i--)
	{
		if (unitQueue[i]->length > 1)
		{
			switch (i)
			{
				case 0:
					first	= unitQueue[i]->length;
					second	= unitQueue[i]->length;
					third	= unitQueue[i]->length;
					printf("1\n");
					break;
				case 1:
					first	= unitQueue[i]->length;
					second	= unitQueue[i-1]->length;
					third	= unitQueue[i-1]->length;
					printf("2\n");
					break;
				default:
					first	= unitQueue[i]->length;
					second	= unitQueue[i-1]->length;
					third	= unitQueue[i-2]->length;
					printf("3\n");
					break;						
			}
			break;
		}
	}

	printf("%d\t%d\t%d\n", first - 1, second - 1, third - 1);
	printf("------------\n");
}



//	it is horizontal Flip-Flop.
void horizontalFlipFlop(IplImage* image, int width, int height)
{
	IplImage* outputImage;
	outputImage = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 3);
	
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
		{
			int temp = (y * width + x) * 3;
			int temp2 = (y * width + width - x - 1) * 3;
			
			outputImage->imageData[temp + 2] = image->imageData[temp2 + 2];
			outputImage->imageData[temp + 1] = image->imageData[temp2 + 1];
			outputImage->imageData[temp + 0] = image->imageData[temp2 + 0];

		}
	}

		
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
		{
			int temp = (y * width + x) * 3;
			image->imageData[temp + 2] = outputImage->imageData[temp + 2];
			image->imageData[temp + 1] = outputImage->imageData[temp + 1];
			image->imageData[temp + 0] = outputImage->imageData[temp + 0];

		}
	}
	cvReleaseImage(&outputImage);
}

//	this function is tracking positions and 
//	print vector that is drection from last position.
coordinate_t checkMoving(IplImage* image, queue_t* movingQueue, coordinate_t point){
	int previousX = -1;
	int previousY = -1;
	int deltaX = 0;
	int deltaY = 0;
	coordinate_t nextPoint;
	
	if( movingQueue->length < 3){
		enqueue(movingQueue, 0, point);		
	}
	else
	{
		dequeue(movingQueue);		
		enqueue(movingQueue, 0, point);		
	}

	for (int i = 0; i < movingQueue->length; i++)
	{		
		node_t* tempNode = nodeAtQueue(movingQueue, i);
		cvCircle(image, cvPoint(tempNode->point.x, tempNode->point.y), 3, CV_RGB(150, 80, 120), -1);

		if(previousX != -1 && previousY != -1)
		{
			deltaX += tempNode->point.x - previousX;
			deltaY += tempNode->point.y - previousY;			
		}

		previousX = tempNode->point.x;
		previousY = tempNode->point.y;
	}
	
	deltaX /= 5;
	deltaY /= 5;
	if ( deltaX + previousX >= 0 && deltaX + previousX < IMG_WIDTH &&
		 deltaY + previousY >= 0 && deltaY + previousY < IMG_HEIGHT)
	{
		cvCircle(image, cvPoint(deltaX + previousX, deltaY + previousY), 3, CV_RGB(255, 255, 0), -1);
	}

	//printf("deltaX : %d\tdeltaY : %d\n", deltaX, deltaY); 

	nextPoint.x = deltaX;
	nextPoint.y = deltaY;

	return nextPoint;
}

void deleteArm(	IplImage* image,
		queue_t* unitQueue[],
		int numberOfQueue, 
		coordinate_t point){
	float sumX = 0;		//sum of unit vector of x
	float sumY = 0;		//sum of unit vector of y
	//float count = 0;	//sum of count is average of unit vectors
	int count = 0;		//number of unit vector
	int check = 1;		//to check the thickest part 
	float scalar2 = 0;	//distance to wrist from center


	for( int i = 0; i < numberOfQueue; i++)
	{
		int max = 0;
		int x, y;
		for( int j = 0; j < unitQueue[i]->length; j++)
		{
			node_t* tempNode = nodeAtQueue(unitQueue[i], j);
			if ( tempNode != NULL)
			{
				if (tempNode->data > max)
				{
					max = tempNode->data;
					x = tempNode->point.x;
					y = tempNode->point.y;
				}
			}
		}



		if (max > 0 && max < 70)
		{
			cvCircle(image, cvPoint(x, y), 3, CV_RGB(0, 255, 255), -1);
			float dX = x - point.x;
			float dY = y - point.y;
			float scalar = sqrt(dX * dX + dY * dY);

			sumX += dX / scalar;
			sumY += dY / scalar;

			count++;
		}

		//if max is bigger than 100, then it is considered the circle in palm.
		if ( max > 100 && check == 1)	
		{
			check = 0;
			node_t* tempNode = NULL;
			if (i + 1 == numberOfQueue)
				tempNode = nodeAtQueue(unitQueue[i], 0);
			else
				tempNode = nodeAtQueue(unitQueue[i + 1], 0);

			int x = tempNode->point.x - point.x;
			int y = tempNode->point.y - point.y;

			scalar2 = sqrt((float)(x*x + y*y));			
		}
	}

	float unitX = sumX / count;
	float unitY = sumY / count;

	point.x += (unitX * scalar2);
	point.y += (unitY * scalar2);

	if ( fabs(unitY) > fabs(unitX))
	{

		for ( int i = 0; i < IMG_HEIGHT; i++){
			for (int j = 0; j < IMG_WIDTH; j++){
				int temp = ( i * IMG_WIDTH + j) * 3;					
				float tempY = -(unitX / unitY) * (j - point.x) + point.y;

				unsigned char red	= image->imageData[temp + 2];
				unsigned char green	= image->imageData[temp + 1];
				unsigned char blue	= image->imageData[temp + 0];

				if ( unitY > 0 ) 
				{
					if ( i > tempY ) 
					{
						if (  (	!(	red		== 0	&&
									green	== 255	&&
									blue	== 255)	&&
								!(	red		== 0	&&
									green	== 255	&&
									blue	== 0)	
							  )
						   )
						{
							image->imageData[temp + 2] = 0;
							image->imageData[temp + 1] = 0;
							image->imageData[temp + 0] = 0;
						}
					}
				}
				else
				{
					if ( i < tempY ) 
					{
						if (  (	!(	red		== 0	&&
									green	== 255	&&
									blue	== 255)	&&
								!(	red		== 0	&&
									green	== 255	&&
									blue	== 0)	
							  )
						   )
						{
							image->imageData[temp + 2] = 0;
							image->imageData[temp + 1] = 0;
							image->imageData[temp + 0] = 0;
						}
					}
				}
			}
		}			
	}
	else
	{
		for ( int i = 0; i < IMG_HEIGHT; i++){
			for (int j = 0; j < IMG_WIDTH; j++){
				int temp = ( i * IMG_WIDTH + j) * 3;					
				float tempX = -(unitY / unitX) * (i - point.y) + point.x;

				unsigned char red	= image->imageData[temp + 2];
				unsigned char green	= image->imageData[temp + 1];
				unsigned char blue	= image->imageData[temp + 0];

				if ( unitX > 0 ) 
				{
					if ( j > tempX ) 
					{
						if (  (	!(	red		== 0	&&
									green	== 255	&&
									blue	== 255)	&&
								!(	red		== 0	&&
									green	== 255	&&
									blue	== 0)	
							  )
						   )
						{
							image->imageData[temp + 2] = 0;
							image->imageData[temp + 1] = 0;
							image->imageData[temp + 0] = 0;
						}
					}
				}
				else
				{
					if ( j < tempX ) 
					{
						if (  (	!(	red		== 0	&&
									green	== 255	&&
									blue	== 255)	&&
								!(	red		== 0	&&
									green	== 255	&&
									blue	== 0)	
							  )
						   )
						{
							image->imageData[temp + 2] = 0;
							image->imageData[temp + 1] = 0;
							image->imageData[temp + 0] = 0;
						}
					}
				}
			}
		}		
	}		
}
























